
import axios from 'axios'
import router from '@/router/index'

import VueRouter from 'vue-router'

import RoleMenuApi from '@/api/menuRequest'

import { resetRouter } from '@/router/index'

export const addRouterEX = () => {

    //获取session路由
    let UserID = JSON.parse(sessionStorage.getItem("roleId"));

    var RouterDatax = [];
    // //如果路由为空,获取路由
    RoleMenuApi.selectRoleAuthority(UserID).then(res => {
        RouterDatax = res.data.data;
        for (let i in RouterDatax) {
            if (String(RouterDatax[i].parentId) == '0' && RouterDatax[i].roleId != null) {
                router.addRoute(
                    'Home',
                    {
                        "path": `/${RouterDatax[i].menuRouter}`,
                        "name": RouterDatax[i].menuRouter,
                        "meta": {
                            "title": RouterDatax[i].menuName
                        },
                        component: () => import(`@/views/${RouterDatax[i].menuRouter}`)
                    }
                )
                // console.log(RouterDatax[i]);
            }
        }

    })
    // resetRouter();
    // console.log(router);
}


// //第二种加法
// export const addRouterEXTwo = () => {
//     //获取session路由
//     var RouterDatax = JSON.parse(sessionStorage.getItem("authority"));
//     //如果路由为空,获取路由
//     if (RouterDatax == null) {
//         axios({
//             method: "get",
//             url: `${process.env.VUE_APP_BASE_URL}AuthorityURL/selectRoleAuthority/1`,
//         }).then(res => {
//             RouterDatax = res.data.data;
//             sessionStorage.setItem("authority", JSON.stringify(res.data.data));
//         })
//     }

//     var path = [
//         {
//             path: "/Home",
//             name: "Home",
//             meta: {
//                 title: "首页"
//             },
//             component: () => import("@/views/home"),
//             children: [
//                 {
//                     path: '/hello',
//                     name: 'hello',
//                     meta: {
//                         title: "测试"
//                     },
//                     component: () => import('@/views/hello.vue'),
//                 },
//             ]
//         },
//     ]
//     for (let i in RouterDatax) {
//         if (String(RouterDatax[i].parentId) == '0' && RouterDatax[i].roleId != null) {
//             let obj = {
//                 "path": `/${RouterDatax[i].menuRouter}`,
//                 "name": RouterDatax[i].menuRouter,
//                 "meta": {
//                     "title": RouterDatax[i].menuName
//                 },
//                 component: () => import(`@/views/${RouterDatax[i].menuRouter}.vue`),
//             }
//             path[0].children.push(obj)

//         }
//     }
//     // console.log(router.getRoutes());

//     router.addRoutes(path);
//     router.options.routes=path;

// }